package com.zw.mes.util;

import com.zw.mes.constant.Constant;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 项目编号转换工具类
 *
 * @Author: China.sgl
 * @Date: 2020/7/30 9:42
 */
@SuppressWarnings("all")
public class ProjectUtils {

    /**
     * 项目编号转8位
     *
     * @param projectCode 项目编号
     * @return 八位项目编号
     */
    public static String projectCodeHandle(String projectCode) {
        String handleCode = "";
        try {
            if (!projectCode.trim().equals(Constant.EMPTY_STRING)) {
                String beginStr = projectCode.trim().toCharArray()[0]+""+projectCode.trim().toCharArray()[1];
                char lastChar = projectCode.trim().toCharArray()[projectCode.trim().length()-1];
                if ("ZW".equals(beginStr.toUpperCase())  || CommonUtils.isNumeric(projectCode.trim()) || lastChar >='a' && lastChar <= 'z' || lastChar >='A' && lastChar <= 'Z') {
                    handleCode = projectCode.trim();
                } else {
                    if (projectCode.trim().length() == 8) {
                        handleCode = projectCode.trim().substring(2);
                    } else {
                        handleCode = projectCode.trim();
                    }
                    String year = new SimpleDateFormat("yyyy").format(new Date());
                    if (Integer.parseInt(year.substring(0, 2)) > 20) {
                        handleCode = year.substring(0, 2) + handleCode;
                    } else {
                        handleCode = "20" + handleCode;
                    }
                }
            }
        }catch (Exception e){
            return projectCode.toUpperCase();
        }
        return handleCode.toUpperCase();
    }


}
